// pages/class/animal/animal.js
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var qqmapsdk;
const app=getApp()
Page({

  /**
   * 页面的初始数据
   */ 
  data: {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          height: res.windowHeight
        })
      }
    });
    qqmapsdk = new QQMapWX({
      key: 'TC5BZ-NY4EK-MAOJM-AIZ27-UKO62-DJBPT'
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  },
  image(e) {
    var index = e.currentTarget.dataset.index;
    var count = 1;
    var that = this;
    wx.chooseImage({
      count: count,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        var tempFilePaths = res.tempFilePaths;
        that.setData({
          tempFilePaths: res.tempFilePaths,
        })
        var imagename=that.data.tempFilePaths[0];
        var testName=imagename.split('/')[3] || imagename.split('/')[2];
        that.setData({
          imagename:testName,
          picType:testName.split('.')[1]
        });
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0], //选择图片返回的相对路径
          encoding: "base64", //这个是很重要的
          success: res => { //成功的回调
            //返回base64格式
            //console.log('data:image/png;base64,' + res.data)
            that.setData({
              picture:res.data
            })
          }
        })
      }
    })
    wx.getLocation({
      type: 'gcj02',
      altitude: true,
      //定位成功，更新定位结果      
      success(res) {
        var latitude = res.latitude
        var longitude = res.longitude
        var speed = res.speed
        var accuracy = res.accuracy
        that.setData({ //赋值
          longitude: longitude,
          latitude: latitude,
          speed: speed,
          accuracy: accuracy
        })
      }, //定位失败回调      
      fail() {
        wx.hideLoading();
        console.log("getLocationFail")
      },
      complete() {
        //隐藏定位中信息进度       
        wx.hideLoading()
      }
    })
  },

  sendCard(){
    var that=this
    if(!that.data.picture || ! that.data.classify|| !that.data.testName||!that.data.introduce){
      wx.showModal({
        title:'请填写完整！'
      })
      return;
    }
    wx.showLoading({
      title: '正在上传...',
    });
    wx.request({
      url:app.globalData.urlHome+'/card/uploadCard',
      data: {
        'openid':app.globalData.openid,
        'latitude':that.data.latitude,
        'longitude':that.data.longitude,
        'picture':that.data.picture,
        'picName':that.data.imagename,
        'picType':that.data.picType,
        'classify':that.data.classify,
        'name':that.data.testName,
        'introduce':that.data.introduce
      },
      header: {
        "accept": "*/*",
        'content-type':'application/json',
        'Authorization':app.globalData.token
      },
      method: 'POST',
      success: (result)=>{
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showToast({
          title: '上传成功！',
          success:()=>{
            setTimeout(()=>{
              wx.navigateBack({
                delta: 0,
              })
            },1000);
          }
        })
        console.log(result.data)
        // that.taskFinish();
      },
      
      fail: ()=>{
        wx.hideLoading({
          success: (res) => {},
        })
        wx.showModal({
          title: '上传失败！',
        })
      }
    });
  },

  getClassify(e){
    this.setData({
      classify: e.detail.value
   });
  },
  getName(e){
    this.setData({
      testName: e.detail.value
   });
  },
  getIntroduce(e){
    this.setData({
      introduce: e.detail.value
   });
  },

  // taskFinish() {
  //   wx.request({
  //     url: app.globalData.urlHome + '/task/finishTask',
  //     header: {
  //       "accept": "*/*",
  //       "content-type": "application/json",
  //       "Authorization": app.globalData.token
  //     },

  //     data: {
  //       "openid": app.globalData.openid,
  //       "type": 5
  //     },
  //     method: 'POST',
  //     success(r) {
  //       console.log(r.data);
  //     },
  //     fail(e) {

  //     }
  //   })
  // },
})